<template>
  <div class="flex h-full w-full min-w-[1200px]">
    <ContentWrap
      class="w-1/6 min-w-[380px]"
      style="margin-right: 2%"
    >
      <div class="px-2">
        <el-select
          v-model="queryParams.tenantIds"
          placeholder="请选择所属企业"
          filterable
          clearable
          @change="handleCompanyFilter"
        >
          <el-option
            v-for="company in tenantList"
            :key="company.id"
            :label="company.enterName"
            :value="company.id"
          >
          </el-option>
        </el-select>
        <div class="card-list-container mt-4">
          <div
            v-for="item in filteredCompanyList"
            :key="item.tenantId"
            :class="{
              active:
                item.tenantId === queryParams.tenantIds
            }"
            @click="chooseCusFun(item)"
          >
            <div class="flex flex-wrap gap-4">
              <el-card shadow="hover" class="company-card">
                <div class="card-content">
                  <img
                    alt=""
                    style="width: 54px; height: 54px"
                    src="@/assets/imgs/account.png"
                  />
                  <div class="card-info">
                    <div class="company-name font-bold">{{
                      item.tenantName
                    }}</div>
                    <div class="balance"
                      >￥{{ item.balanceTotal }}</div
                    >
                  </div>
                </div>
              </el-card>
            </div>
          </div>
        </div>
      </div>
    </ContentWrap>
    <section
      class="flex flex-col h-full flex-1 overflow-auto"
    >
      <div class="flex justify-between mb-16px">
        <el-form
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="68px"
        >
          <el-form-item
            label="客户企业"
            prop="customerName"
          >
            <el-input
              v-model="customerName"
              :maxlength="50"
              placeholder="请输入客户企业名称"
              @keyup.enter="handleQuery"
              class="!w-200px"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleQuery"
              >搜索</el-button
            >
            <el-button @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <div>
          <el-button @click="toggle">
            <el-tooltip
              effect="dark"
              content="全屏"
              placement="top-start"
            >
              <div style="width: 22px; height: 22px">
                <svg
                  xmlns="http://www.w3.org/2000/svg"
                  viewBox="0 0 24 24"
                  fill="currentColor"
                >
                  <path
                    d="M8 3V5H4V9H2V3H8ZM2 21V15H4V19H8V21H2ZM22 21H16V19H20V15H22V21ZM22 9H20V5H16V3H22V9Z"
                  ></path>
                </svg>
              </div>
            </el-tooltip>
          </el-button>
        </div>
      </div>
      <el-table
        class="flex-1"
        ref="singleTableRef"
        v-loading="loading"
        :data="list"
        border
        stripe
        @selection-change="selectionChange"
        :header-cell-style="{
          background: '#F3F4F7',
          color: '#555'
        }"
        height="100%"
      >
        <el-table-column
          label="序号"
          type="index"
          prop="id"
          width="55"
          fixed="left"
        />
        <el-table-column
          label="所属企业"
          align="left"
          prop="tenantName"
          min-width="150"
        />
        <el-table-column
          label="客户企业"
          align="left"
          prop="customerName"
          min-width="150"
        />
        <el-table-column
          label="现金余额(元)"
          align="left"
          prop="cashBalance"
          min-width="150"
        />
        <el-table-column
          label="承兑余额(元)"
          align="left"
          prop="acceptanceBalance"
          min-width="150"
        />
        <el-table-column
          label="一年承兑余额(元)"
          align="left"
          prop="oneYearAcceptanceAccountBalance"
          min-width="150"
        />
        <el-table-column
          label="操作"
          align="left"
          prop="roseAndFallIndex"
          width="100"
          fixed="right"
        >
          <template #default="{ row }">
            <el-button
              link
              type="primary"
              @click="openForm(1, row)"
              v-hasPermi="['financial:account:record']"
            >
              交易记录
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </section>
  </div>
</template>
<script setup lang="ts" name="individualAccount">
import { useFullscreen } from '@vueuse/core'
import * as Utils from '@/utils/dict'
import * as authorizationManageApi from '@/api/authorizationManage'
import * as individualAccountManageApi from '@/api/individualAccount'
import { getTenantId } from '@/utils/auth'
import * as salesPlanApi from '@/api/salesPlan'

const loading = ref(false)
const total = ref(0)
const list = ref([])
const queryFormRef = ref()
const singleTableRef = ref()
const message = useMessage()
const { isFullscreen, toggle } =
  useFullscreen(singleTableRef)
const { DICT_TYPE, getIntDictOptions } = reactive(Utils)
const queryParams = ref({
  pageNo: 1,
  pageSize: 10,
  dispatchTenantId: getTenantId(),
  tenantIds: undefined
})
const customerName = ref('')

/** 所属企业下拉列表数据 */
const tenantList = ref([])
const getOutTenantList = async () => {
  const res =
    await authorizationManageApi.getOutTenantListByIdentity(
      { identity: 1 }
    )
  tenantList.value = res || []
}

/** 所属企业余额合计列表数据 */
const filteredCompanyList = ref([])
const getOutCompanyList = async () => {
  const res =
    await individualAccountManageApi.getOutCompanyList(
      queryParams.value
    )
  filteredCompanyList.value = res || []
}

onMounted(() => {
  getOutTenantList()
  getOutCompanyList()
  getList()
})

//根据下拉框选中筛选卡片列表
const handleCompanyFilter = (value) => {
  console.log(value)
  console.log(queryParams.value)
  getOutCompanyList()
  getList()
}

const chooseCusFun = (item) => {
  queryParams.value.tenantIds = item.tenantId
  getList()
}
const router = useRouter()
const getList = async () => {
  loading.value = true
  try {
    const res =
      await individualAccountManageApi.getDispatchCustomerBalancePage(
        queryParams.value,
        customerName.value
      )
    if (res.code === 0) {
      total.value = res.data?.total || 0
      list.value = res.data?.list || []
    } else if (res.code === 100910003) {
      loading.value = false
      await message.confirm(res.msg)
      refreshAuthorize()
    } else if (res.code === 100910004) {
      loading.value = false
      await message.confirm(res.msg)
      router.push({
        path: '/authManageCatalog/authorizationManage'
      })
    } else {
      message.error(res.msg)
    }
  } finally {
    loading.value = false
  }
}
const refreshAuthorize = async () => {
  await salesPlanApi.refreshAuthorize()
  getOutTenantList()
  getOutCompanyList()
  getList()
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  customerName.value = ''
  queryParams.value.tenantIds = undefined
  getOutCompanyList()
  handleQuery()
}

const emit = defineEmits(['subPage'])
const openForm = (index: number, row?: object) => {
  emit('subPage', index, row)
}
</script>
<style lang="scss" scoped>
:deep(.el-table__header-wrapper .el-checkbox__inner) {
  display: none !important;
}

:deep(.el-select) {
  width: 100%;
  max-width: 320px;

  .el-input__wrapper {
    width: 100%;
  }
}
.el-table {
  :deep(.el-button) {
    height: 30px !important;
    line-height: 30px !important;
  }
}

.el-select-dropdown__item {
  border-radius: 4px;
  padding: 0px 12px;
}

.card-header {
  font-size: 16px;
  display: flex;
  align-items: center;
}

.company-card {
  width: 360px;
  min-height: 94px;
  margin-top: 20px;
  background: linear-gradient(
    100deg,
    rgba(78, 211, 255, 0.2),
    rgba(64, 158, 254, 0.2) 50%
  );
  border-radius: 4px;
  border: 1px solid transparent;

  :deep(.el-card__body) {
    padding: 0;
    height: 94px;
  }
}
.card-content {
  display: flex;
  align-items: center;
  min-height: 94px;
  width: 100%;
  padding: 15px;
}

.card-info {
  display: flex;
  flex-direction: column;
  justify-content: center;
  width: 100%;
  min-height: 64px;
  padding-left: 10px;
  gap: 4px;

  .company-name {
    font-size: 18px;
    color: #303133;
    font-family: PingFang SC, PingFang SC-500;
    text-align: left;
    font-weight: 500;
    line-height: 1.2;
    word-break: break-all;
    white-space: normal;
  }

  .balance {
    font-size: 14px;
    font-family: Helvetica Neue, Helvetica Neue-400;
    font-weight: 400;
    text-align: left;
    color: #606266;
    line-height: 1.2;
    word-break: break-all;
    white-space: normal;
  }
}

.card-list-container {
  max-height: calc(100vh - 220px);
  overflow-y: auto;
  padding-right: 15px;
  padding-bottom: 60px;

  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-track {
    background: #f1f1f1;
  }

  &::-webkit-scrollbar-thumb {
    background: #e2e2e2;
    border-radius: 2px;
  }

  &::-webkit-scrollbar-thumb:hover {
    background: #aaa;
  }
}

.card-list-container .active {
  .company-card {
    border: 1px solid #409efe;
  }
}
:deep(.el-select) {
  width: 100%;

  .el-input__wrapper {
    width: 100%;
  }
}
:deep(.el-select-dropdown) {
  min-width: 100%;
  max-width: 100%;

  .el-select-dropdown__item {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
</style>
